package com.cardone.platform.authority.controller;

import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;

import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cardone.context.Contexts;
import com.cardone.common.dto.PaginationDto;
import com.cardone.common.dto.ReturnDataDto;
import com.cardone.common.util.ReturnDataUtils;
import com.cardone.context.ContextHolder;
import com.cardone.platform.authority.dto.PermissionDto;
import com.cardone.platform.authority.service.PermissionService;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

/**
 * 许可
 *
 * @author yaohaitao
 */
@Deprecated
@Setter
@Slf4j
@RequestMapping("/authority/permission/")
@Controller
public class PermissionController {

	/**
	 * 许可：删除
	 * 
	 * @param deletePermission
	 *            许可对象
	 * @return 处理结果，json格式
	 */
	@Deprecated
	@RequestMapping(value = { "delete.json" }, method = RequestMethod.POST)
	@ResponseBody
  public Callable<ReturnDataDto<String>> deleteJson(final PermissionDto deletePermission) {
		return new Callable<ReturnDataDto<String>>() {
			public ReturnDataDto<String> call() throws Exception {
				try {
					ContextHolder.getBean(PermissionService.class).deleteByIds(deletePermission);
					
					return ReturnDataUtils.newReturnData();
				} catch (final Exception e) {
					return ReturnDataUtils.newReturnData(e, log);
				}
			}
		};
	}

	/**
	 * 许可：编辑
	 * 
	 * @param findPermission
	 *            许可对象
	 * @return 实体对象集，json格式
	 */
	@Deprecated
	@RequestMapping(value = { "findById.json" })
	@ResponseBody
	public Callable<ReturnDataDto<PermissionDto>> findByIdJson(final PermissionDto findPermission, final Model model) {
		return new Callable<ReturnDataDto<PermissionDto>>() {
			@Override
			public ReturnDataDto<PermissionDto> call() throws Exception {
				try {
					final PermissionDto permission = ContextHolder.getBean(PermissionService.class).findById(PermissionDto.class, findPermission);

					return ReturnDataUtils.newReturnData(permission);
				} catch (final Exception e) {
					return ReturnDataUtils.newReturnData(e, PermissionController.log);
				}
			}
		};
	}

	/**
	 * 许可：分页查询
	 * 
	 * @param paginationPermission
	 *            许可对象
	 * @return 分页，json格式
	 */
	@Deprecated
	@RequestMapping(value = { "paginationByLikeCode.json" })
	@ResponseBody
  public Callable<ReturnDataDto<PaginationDto<PermissionDto>>> paginationByLikeCodeJson(final PermissionDto paginationPermission) {
		return new Callable<ReturnDataDto<PaginationDto<PermissionDto>>>() {
			public ReturnDataDto<PaginationDto<PermissionDto>> call() throws Exception {
				try {
					final PaginationDto<PermissionDto> permissionPagination = ContextHolder.getBean(PermissionService.class).paginationByLikeCode(PermissionDto.class, paginationPermission);
				
					return ReturnDataUtils.newReturnData(permissionPagination);
				} catch (final Exception e) {
					return ReturnDataUtils.newReturnData(e, log);
				}
			}
		};
	}

	/**
	 * 许可：分页查询
	 * 
	 * @param paginationPermission
	 *            许可对象
	 * @param model
	 *            上下文映射
	 */
	@Deprecated
	@RequestMapping(value = { "pagination.html" })
  public void paginationHtml(final PermissionDto paginationPermission, final Model model) {
		final PaginationDto<PermissionDto> permissionPagination = ContextHolder.getBean(PermissionService.class).paginationByLikeCode(PermissionDto.class, paginationPermission);
		
		model.addAttribute("permissionPagination", permissionPagination);
	}
	
	/**
	 * 许可：分页查询
	 * 
	 * @param findPermission
	 *            许可对象
	 * @param model
	 *            上下文映射
	 */
	@Deprecated
	@RequestMapping(value = { "edit.html" })
  public void editHtml(final PermissionDto findPermission, final Model model) {
		final PermissionDto permission = ContextHolder.getBean(PermissionService.class).findById(PermissionDto.class, findPermission);
		
		model.addAttribute("permission", permission);
	}

	/**
	 * 许可：查询
	 * 
	 * @param findListPermission
	 *            许可对象
	 * @return 许可对象集合，json格式
	 */
	@Deprecated
	@RequestMapping(value = { "findListByLikeCode.json" })
	@ResponseBody
  	public Callable<ReturnDataDto<List<PermissionDto>>> findListByLikeCodeJson(final PermissionDto findListPermission) {
		return new Callable<ReturnDataDto<List<PermissionDto>>>() {
			public ReturnDataDto<List<PermissionDto>> call() throws Exception {
				try {
					final List<PermissionDto> permissionList = ContextHolder.getBean(PermissionService.class).findListByLikeCode(PermissionDto.class, findListPermission);
				
					return ReturnDataUtils.newReturnData(permissionList);
				} catch (final Exception e) {
					return ReturnDataUtils.newReturnData(e, log);
				}
			}
		};
	}

	/**
	 * 许可：保存
	 * 
	 * @param savePermission
	 *            许可对象
	 * @return 处理结果，json格式
	 */
	@Deprecated
	@RequestMapping(value = { "save.json" })
	@ResponseBody
  public Callable<ReturnDataDto<String>> saveJson(final PermissionDto savePermission) {
		return new Callable<ReturnDataDto<String>>() {
			public ReturnDataDto<String> call() throws Exception {
				try {
					ContextHolder.getBean(PermissionService.class).saveByIdOrCode(PermissionDto.class, savePermission);
					
					return ReturnDataUtils.newReturnData();
				} catch (final Exception e) {
					return ReturnDataUtils.newReturnData(e, log);
				}
			}
		};
	}
}
